package com.externalapi;

import java.io.*;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import com.enums.TargetApi;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpResponse;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.result.ActionResult;
import com.result.ApiResult;
import com.utils.Constants;

public class GoogleDriveApi extends ServiceApi {

	public static Drive service;
	private Context context;

	private static final String className = GoogleDriveApi.class
			.getSimpleName();

	@Override
	public ApiResult insertFile(TargetApi target, Intent file) {
		String methodName = "insertFile()";
		Log.i(className, methodName);
		final Uri uri = file.getData();
		String url = uri.getPath();
		Log.i(className, uri.getPath());
		Uri fileUri = Uri.fromFile(new java.io.File(url));
		String mimeType = super.getMimeTypeFromURI(url);
		ApiResult apiResult = new ApiResult();
		try {
			// File's binary content
			java.io.File fileContent = new java.io.File(fileUri.getPath());
			FileContent mediaContent = new FileContent(mimeType, fileContent);

			// File's metadata.
			File body = new File();
			body.setTitle(fileContent.getName());
			body.setMimeType(mimeType);
			if (service == null) {
				apiResult.setSuccess(false);
				apiResult
						.setErrorCode(Constants.REQUEST_GOOGLE_CREDENTIAL_ERROR);
				return apiResult;
			}
			File fileUploaded = service.files().insert(body, mediaContent)
					.execute();
			if (fileUploaded != null) {
				Log.i(className, "fileUploaded success");
				apiResult.setSuccess(true);
			}
		} catch (UserRecoverableAuthIOException e) {
			apiResult.setErrorCode(Constants.REQUEST_AUTHORIZATION_ERROR);
			Log.i(className, "Error request authorization: " + e);
		} catch (IOException e) {
			apiResult.setErrorCode(Constants.UNKNOW_ERROR_UPLOADING);
			Log.e(className, "Error uploading file: " + e);
		}
		return apiResult;
	}

	@Override
	public ApiResult downloadFile(TargetApi target, Object file) {
		String methodName = "downloadFile()";
		Log.i(className, methodName);

		ApiResult apiResult = new ApiResult();
		Intent intentFile = (Intent) file;
		File fileToDownload = null;
		try {
			fileToDownload = getFileFromDrive(intentFile
					.getStringExtra(Constants.fileId));
			if (fileToDownload.getDownloadUrl() != null
					&& fileToDownload.getDownloadUrl().length() > 0) {
				try {
					HttpResponse resp = service
							.getRequestFactory()
							.buildGetRequest(
									new GenericUrl(fileToDownload
											.getDownloadUrl())).execute();
					ActionResult result = saveFileOnDevice(resp.getContent());
					if (result.isSuccess()) {
						apiResult.setSuccess(true);
					} else {
						apiResult.setSuccess(false);
						apiResult
								.setErrorCode(Constants.SAVING_FILE_LOCAL_ERROR);
					}
				} catch (IOException e) {
					apiResult.setErrorCode(Constants.UNKNOW_ERROR_DOWNLOADING);
				}
			}
		} catch (IOException e) {
			apiResult.setErrorCode(Constants.UNKNOW_ERROR_DOWNLOADING);
		}
		return apiResult;
	}

	private File getFileFromDrive(String fileId) throws IOException {
		String methodName = "getFileFromDrive()";
		Log.i(className, methodName);
		File file = null;
		file = service.files().get(fileId).execute();
		return file;
	}

	private ActionResult saveFileOnDevice(InputStream file) {

		return null;
	}

	@Override
	public ApiResult deleteFile(TargetApi target, Object file) {
		String methodName = "deleteFile()";
		Log.i(className, methodName);
		Intent intentFile = (Intent) file;
		ApiResult apiResult = new ApiResult();
		try {
			service.files().delete(intentFile.getStringExtra(Constants.fileId));
			apiResult.setSuccess(true);
		} catch (IOException e) {
			apiResult.setErrorCode(Constants.UNKNOW_ERROR_DELETING);
		}
		return apiResult;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

}
